<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?><?php defined( '_SEXEC' ) or die( 'Restricted access' ); ?>
<?php
	if(!isset($_SESSION['username']) && !isset($_SESSION['usertype']))
		header("location: index.php");
?>


<?php
$company_arr = array();
$sql = "select id, companyname, companycode, invoice_no from companies where status = 1 order by companyname";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
	$company_arr[$row['id']] = $row['companyname'];
}

?>

<script language="javascript">

	$(function () { // this line makes sure this code runs on page load
		$('#select_all').click(function () {
			$(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
		});
		$('#button').click(function () {
			var selectedItems = new Array();
			var from = $('#period_from').val();
			var to = $('#period_to').val();
			$("input[@name='company_ids[]']:checked").each(function() {selectedItems.push($(this).val());});
			 
			if (selectedItems.length == 0) {
				alert("Please select companies(s).");
			} else if (from == '') {
				alert("Please select period from.");
				$('#period_from').focus();
			} else if (to == '') {
				alert("Please select period to.");
				$('#period_to').focus();
			} else {
				$("#myform").submit();
			}
			
			    
			    
		});
	});
	
	
</script>


<div id="title">Company Statement</div>

<form id="myform" method="post" action="MonthReport.php" target="_blank">
<table border="0" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td align=right valign=top>Select Company:</td>
		<td colspan=3>
			<div class="people-chooser clearfix">
				<fieldset>
					<div class="select-all-people">
						<input type="checkbox" name="select_all" id="select_all" />
						<span>Select All</span>
					</div>
					<div class="table-wrapper">
						<table id="contacts-table">
							<?php
							foreach ($company_arr as $k => $v) {
							$st = ($k == $company_ids[$k]) ? "checked" : "";
							?>
							<tr class="contact">
								<td><input type="checkbox" name="company_ids[<?php echo $k ?>]" id="company_ids" value="<?php echo $k ?>" <?php echo $st ?> /></td>
								<td><?php echo $v ?></td>
							</tr>
							<?php
							}
							?>  
						</table>
					</div>
				</fieldset> 
			</div>
		</td>
	</tr>
	<tr>
		<td align=right>Period From:</td>
		<td><input name="period_from" id="period_from" type="text" value="<?php echo $period_from?>" onfocus="popcal(this)" /></td>
		<td align=right>To:</td>
		<td><input name="period_to" id="period_to" type="text" value="<?php echo $period_to?>" onfocus="popcal(this)" /></td>
	</tr>
	<tr>
		<td colspan="4" align="center">
			<input type="button" name="button" id="button" value="View" class='button'/>
		</td>
	</tr>	
</table>
</form>